package st.business.tepth.business.maintainplan;

import android.app.DatePickerDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.business.bean.MaintainPlanBean;
import st.business.tepth.business.businfo.BusPagerAdapter;
import st.business.tepth.business.businfo.FragmentWarnHistory;
import st.business.tepth.business.businfo.FragmentWorkHistory;
import st.shadow.library.core.base.fragment.BaseFragment;
import st.shadow.library.ui.indicator.magicindicator.MagicIndicator;
import st.shadow.library.ui.indicator.magicindicator.ViewPagerHelper;
import st.shadow.library.ui.indicator.magicindicator.buildins.UIUtil;
import st.shadow.library.ui.indicator.magicindicator.buildins.commonnavigator.CommonNavigator;
import st.shadow.library.ui.indicator.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import st.shadow.library.ui.indicator.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import st.shadow.library.ui.indicator.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import st.shadow.library.ui.indicator.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import st.shadow.library.ui.indicator.magicindicator.buildins.commonnavigator.titles.ClipPagerTitleView;

/**
 * @author zhangyu
 * @date 2019/6/4
 */
public class FragmentBusInfo extends BaseFragment {
    public static FragmentBusInfo newInstance(MaintainPlanBean planBean) {
        Bundle bundle = new Bundle();
        bundle.putParcelable("info", planBean);
        FragmentBusInfo delegate = new FragmentBusInfo();
        delegate.setArguments(bundle);
        return delegate;
    }

    @BindView(R2.id.tv_bus_licence)
    TextView mTvLicence;
    @BindView(R2.id.tv_bus_company)
    TextView mTvCompany;
    @BindView(R2.id.tv_register_time)
    TextView mTvRegisterTime;
    @BindView(R2.id.tv_bus_type)
    TextView mTvBusType;
    @BindView(R2.id.tv_gas_use)
    TextView mTvGasUse;
    @BindView(R2.id.tv_oil_use)
    TextView mTvOilUse;
    @BindView(R2.id.tv_last_mileage)
    TextView mTvLastMileage;
    @BindView(R2.id.tv_total_mileage)
    TextView mTvTotalMileage;
    @BindView(R2.id.indicator)
    MagicIndicator mMagicIndicator;
    @BindView(R2.id.et_date_start)
    TextView mTvDateStart;
    @BindView(R2.id.et_date_end)
    TextView mTvDateEnd;
    @BindView(R2.id.view_pager)
    ViewPager mViewPager;
    private MaintainPlanBean mMaintainPlanBean;
    private int mCurrentIndex = 0;
    private List<Fragment> mFragments = new ArrayList<>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mMaintainPlanBean = bundle.getParcelable("info");
        }
    }

    @Override
    public Object setLayout() {
        return R.layout.fragment_bus_info;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        if (mMaintainPlanBean != null) {
            mTvLicence.setText(mMaintainPlanBean.getName());
            mTvCompany.setText(String.format("所属公司：%s",
                    mMaintainPlanBean.getCompany()));
            mTvRegisterTime.setText(String.format("登记日期：%s",
                    mMaintainPlanBean.getRegister()));
            mTvBusType.setText(String.format("车辆型号：%s",
                    mMaintainPlanBean.getType()));
            String gasType = mMaintainPlanBean.getFuelType();
            String fuelPerKm = mMaintainPlanBean.getFuelPerKm();
            String engineOil = mMaintainPlanBean.getEngineOil();
            String kmAfterLastVm = mMaintainPlanBean.getKmAfterLastVm();
            String kmTotal = mMaintainPlanBean.getKmTotal();
            mTvGasUse.setText(String.format("上月%s耗量：%s", gasType, fuelPerKm));
            mTvOilUse.setText(String.format("上月机油耗量：%s", engineOil));
            mTvLastMileage.setText(String.format("距上次保养里程：%s", kmAfterLastVm));
            mTvTotalMileage.setText(String.format("总里程：%s",
                    mMaintainPlanBean.getMileage() + "公里"));
        }
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        FragmentWorkHistory fragmentWorkHistory = FragmentWorkHistory.newInstance();
        FragmentWarnHistory fragmentWarnHistory = FragmentWarnHistory.newInstance();
        mFragments.add(fragmentWorkHistory);
        mFragments.add(fragmentWarnHistory);
        fragmentWorkHistory.search(mMaintainPlanBean.getNo(), "", "");
        fragmentWarnHistory.search(mMaintainPlanBean.getNo(), "", "");
        mViewPager.setAdapter(new BusPagerAdapter(getChildFragmentManager(), mFragments));

        CommonNavigator commonNavigator = new CommonNavigator(_mActivity);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return 2;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                ClipPagerTitleView clipPagerTitleView = new ClipPagerTitleView(context);
                clipPagerTitleView.setText(index == 0 ? "作业记录" : "报警记录");
                clipPagerTitleView.setTextColor(Color.parseColor("#8fa3b7"));
                clipPagerTitleView.setClipColor(Color.parseColor("#333333"));
                clipPagerTitleView.setTextSize(22);
                clipPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mViewPager.setCurrentItem(index);
                        mCurrentIndex = index;
                    }
                });
                return clipPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                float navigatorHeight = context.getResources()
                        .getDimension(R.dimen.common_navigator_height);
                float borderWidth = UIUtil.dip2px(context, 1);
                float lineHeight = navigatorHeight - 2 * borderWidth;
                indicator.setLineHeight(lineHeight);
                indicator.setRoundRadius(2);
                indicator.setYOffset(borderWidth);
                indicator.setColors(Color.parseColor("#f1b647"));
                return indicator;
            }
        });
        mMagicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(mMagicIndicator, mViewPager);
    }

    @OnClick(R2.id.et_date_start)
    public void onChooseDateStart() {
        final Calendar calendar = Calendar.getInstance();
        DatePickerDialog dialog = new DatePickerDialog(_mActivity,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        String date = year + "-" + ((month + 1) < 10 ? "0" + (month + 1) : (month + 1)) +
                                "-" + (dayOfMonth < 10 ? ("0" + dayOfMonth) : dayOfMonth);
                        mTvDateStart.setText(date);
                        mTvDateStart.setTextColor(Color.parseColor("#333333"));
                    }
                },
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH));
        dialog.show();
    }

    @OnClick(R2.id.et_date_end)
    public void onChooseDateEnd() {
        final Calendar calendar = Calendar.getInstance();
        DatePickerDialog dialog = new DatePickerDialog(_mActivity,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        String date = year + "-" + ((month + 1) < 10 ? "0" + (month + 1) : (month + 1)) +
                                "-" + (dayOfMonth < 10 ? ("0" + dayOfMonth) : dayOfMonth);
                        mTvDateEnd.setText(date);
                        mTvDateEnd.setTextColor(Color.parseColor("#333333"));
                    }
                },
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH));
        dialog.show();
    }

    @OnClick(R2.id.iv_search)
    public void onSearch() {
        Fragment fragment = mFragments.get(mCurrentIndex);
        if (mCurrentIndex == 0) {
            ((FragmentWorkHistory) fragment)
                    .search(mMaintainPlanBean.getNo(), "请选择开始日期"
                                    .equals(mTvDateStart.getText().toString()) ? "" :
                                    mTvDateStart.getText().toString(),
                            "请选择结束日期".equals(mTvDateEnd.getText().toString()) ?
                                    "" : mTvDateEnd.getText().toString());
        } else {
            ((FragmentWarnHistory) fragment)
                    .search(mMaintainPlanBean.getNo(), "请选择开始日期"
                                    .equals(mTvDateStart.getText().toString()) ? "" :
                                    mTvDateStart.getText().toString(),
                            "请选择结束日期".equals(mTvDateEnd.getText().toString()) ?
                                    "" : mTvDateEnd.getText().toString());
        }
    }
}
